# -*- coding: utf-8 -*-
from sqlalchemy.schema import *
from sqlalchemy.types import *
from sqlalchemy.orm import *
from pythonblog.model import meta
from pythonblog.lib.helpers import now
import datetime


class Post(meta.Base):
    __tablename__ = 'post'
    
    id = Column(Integer, primary_key=True)
    title = Column(Unicode(50), nullable=False)
    abstract = Column(UnicodeText)
    content = Column(UnicodeText)
    createDate = Column(DateTime, nullable=False)
    updateDate = Column(DateTime)
    status = Column(SmallInteger, nullable=False)
    type = Column(SmallInteger, nullable=False)
    
    section_id = Column(Integer, ForeignKey('section.id'), nullable=False)
    #section = relation('Section')
    
    user_id = Column(Integer, ForeignKey('user_profile.id'), nullable=False)
    #user = relation('UserProfile')
    
    viewTimes = Column(Integer, nullable=False)
    #comments = relation('Comment', lazy='False', order_by='desc(Comment.createDate)',
    #                    cascade='all,delete-orphan')
    
    #promoters = relation('VisitorTrace',
    #                     ForeignKey('visitor_trace.id'),
    #                     lazy='dynamic', order_by=['userID', 'userIP'])
                        
    tags = relation('Tag', lazy='False', order_by='Tag.name', cascade='all,delete-orphan')
    
    def get_tags_display(self):
        return ', '.join(t.name for t in self.tags)

    tags_display = property(get_tags_display)

    def __init__(self):
        self.createDate = now()
        self.viewTimes = 0
        #self.status = 1  1: 发表 2: 草稿
        #self.type = 1  1: 原创 2: 转载 3:　新闻 4: 翻译
        
    def __repr__(self):
        return '<Post "%s">' % self.title

class Comment(meta.Base):
    __tablename__ = 'comment'
    
    id = Column(Integer, primary_key=True)
    content = Column(UnicodeText, nullable=False)
    createDate = Column(DateTime, nullable=False)
    
    user_id = Column(Integer, ForeignKey('user_profile.id'))
    #user = relation('UserProfile')
    
    post_id = Column(Integer, ForeignKey('post.id'), nullable=False)
    #post = relation('Post')
    
    def __init__(self):
        self.createDate = now()
        
    def __repr__(self):
        return '<Comment on "%s">' % self.id
    
class VisitorTrace(meta.Base):
    __tablename__ = 'visitor_trace'
    
    id = Column(Integer, primary_key=True)
    post_id = Column(Integer, ForeignKey('post.id'))
    userIdentifier = Column(String(50))
    promote = Column(Integer)
    createDate = Column(DateTime)

    def __init__(self):
        self.createDate = now()

    def __repr__(self):
        return '<VisitorTrace "%s">' % self.id

class Tag(meta.Base):
    __tablename__ = 'tag'
    
    name = Column(Unicode(20), primary_key=True)
    post_id = Column(Integer, ForeignKey('post.id'), primary_key=True)

    def __repr__(self):
        return '<Tag "%s">' % self.name
    
class RecommandPost(meta.Base):
    """
    For generating the table
    """
    __tablename__ = 'recommand_post'

    id = Column(Integer, primary_key=True)
    post_id = Column(Integer)